package tech.daoxuan.security.browser;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
import org.springframework.social.security.SpringSocialConfigurer;
import tech.daoxuan.security.core.properties.SecurityProperties;
import tech.daoxuan.security.core.validate.code.ValidateCodeFilter;

import javax.sql.DataSource;

@Configuration
public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private SpringSocialConfigurer daoxuanSocialSecurityConfig;

    /**
     * 如果 有自己的加密解密 工具 MD5什么的 可以实现 PasswordEncoder接口 即可
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    };


    @Autowired
    private AuthenticationSuccessHandler daoxuanAuthenticationSeccessHandler;

    @Autowired
    private AuthenticationFailureHandler daoxuanAuthenticationFailureHandler;

   @Autowired
   private SecurityProperties securityProperties;



/*------记住我---start----*/

    /**
     * 导入这个接口 就是为了找到 token 写入到 UserDetails中
     */
    @Autowired
    private UserDetailsService userDetailsService;
    /**
     * 在demo项目中已经给配置了
     */
   @Autowired
   private DataSource dataSource;

   @Bean
   public PersistentTokenRepository persistentTokenRepository(){
       JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
        jdbcTokenRepository.setDataSource(dataSource);
        jdbcTokenRepository.setCreateTableOnStartup(true);
       return jdbcTokenRepository;
   }
/*--------end------------*/
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        ValidateCodeFilter validateCodeFilter = new ValidateCodeFilter();
        validateCodeFilter.setAuthenticationFailureHandler(daoxuanAuthenticationFailureHandler);
        validateCodeFilter.setSecurityProperties(securityProperties);
        validateCodeFilter.afterPropertiesSet();
        //super.configure(http);
//        http.formLogin();
        http.addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class)
                .formLogin()
                .loginPage("/authentication/require")
                .loginProcessingUrl("/authentication/form")//指明登陆的路径
                .successHandler(daoxuanAuthenticationSeccessHandler)
                .failureHandler(daoxuanAuthenticationFailureHandler)
                .and()
                .apply(daoxuanSocialSecurityConfig)
                .and()
                .rememberMe()
                .tokenRepository(persistentTokenRepository())
                .tokenValiditySeconds(securityProperties.getBrowser().getRememberMeSeconds())
                .userDetailsService(userDetailsService)
                .and()
                .authorizeRequests()
                .antMatchers("/code/image","/authentication/require",securityProperties.getBrowser().getLoginPage()).permitAll()//登陆页面不做权限校验
                .anyRequest()
                .authenticated()
                .and().csrf().disable()
        ;
    }
}
